import { INestApplication, Logger } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'

// import { API_SECURITY_AUTH } from './common/decorators/swagger.decorator'
// import { CommonEntity } from './common/entity/common.entity'
import { ResOp, TreeResult } from './common/model/response.model'
import { ConfigKeyPaths } from './config'
import { IAppConfig } from './config/app.config'
import { ISwaggerConfig } from './config/swagger.config'
import { API_SECURITY_AUTH } from './common/interceptors/swagger.decorator'
import { knife4jSetup } from 'nest-knife4j'
// import { Pagination } from './helper/paginate/pagination'

export function setupSwagger(
  app: INestApplication,
  configService: ConfigService<ConfigKeyPaths>,
): void {
  const { name, port } = configService.get<IAppConfig>('app')!
  const { enable, path } = configService.get<ISwaggerConfig>('swagger')!

  if (!enable)
    return

  const documentBuilder = new DocumentBuilder()
    .setTitle(name)
    .setDescription(`${name} API 接口文档`)
    .setVersion('1.0')

  // 添加安全认证
  documentBuilder.addSecurity(API_SECURITY_AUTH, {
    description: '输入令牌（Enter the token）',
    type: 'http',
    scheme: 'bearer',
    bearerFormat: 'JWT',
  })

  const document = SwaggerModule.createDocument(app, documentBuilder.build(), {
    ignoreGlobalPrefix: false,
    extraModels: [ResOp, TreeResult],
  })
  SwaggerModule.setup(path, app, document, {
    swaggerOptions: {
      persistAuthorization: true, // 保持登录
    },
    jsonDocumentUrl: '/openJson',
  })


  knife4jSetup(app, [{
    name: 'v1',
    url: `/openJson`,      // 必须和 jsonDocumentUrl 一致
    swaggerVersion: '1.0',
    location: `/openJson`, // 同上
  }]);


}
